package com.shigen.shop.controller;

import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知控制类
 *
 * @author shigenfu
 * @date 2022/7/24 4:00 下午
 */
@RequestMapping(value = "notify")
@RestController
@Api(tags = "通知控制类")
public class NotifyController {

  @Resource
  private Producer captchaProducer;

  @GetMapping(value = "captcha")
  @ApiOperation(value = "绘制图形验证码")
  public void getCaptcha(HttpServletResponse response) throws IOException {
    response.setHeader("Cache-Control", "no-store, no-cache");
    response.setContentType("image/jpeg");

    //生成文字验证码
    String text = captchaProducer.createText();
    //生成图片验证码
    BufferedImage image = captchaProducer.createImage(text);
    // 输出生成的text
    System.out.println(text);

    ServletOutputStream out = response.getOutputStream();
    ImageIO.write(image, "jpg", out);
  }


}
